class DLSTS   #盘扣类

    def initialize()   #构造函数

        @model = Sketchup.active_model
        @group = @model.active_entities.add_group

        @data = [
            ['BaseCollar','c:/baseCollar.skp',[-27.35.mm,-27.35.mm,-100.mm]],
            ['ConnectPlate','c:/connectingPlate.skp',[-69.5.mm,-69.5.mm,-5.mm]],
            ['LedgerEnd','c:/ledgerEnd.skp',[24.mm,-27.5.mm,-60.mm]],
            ['base','c:/base.skp',[-80.mm,-80.mm,0.mm]],
            ['adjustNut','c:/adjustNut.skp',[-80.mm,-33.mm,0.mm]],
            ['head','c:/head.skp',[-75.mm,-75.mm,-5.mm]]
        ]
        
    end

    def createStandard(mod,groupW)             #创建立杆，模数(3,2.5,2,1.5,1,0.6,0.2)

        group = groupW.entities.add_group
        #group = @group
        entities = group.entities

        lineAxis = entities.add_line([0,0,0],[0,0,mod.m])
        circTb = createCircularTube(mod.m,[0,0,0],Z_AXIS,group)
        baseCollar = importDLSTSComponentTP1('BaseCollar',group)

        num = (mod/0.5).to_i
        if num == 1

            connectPlate = importDLSTSComponentTP1('ConnectPlate',group)  
            entities.transform_entities(Geom::Transformation.new([0,0,300.mm]), connectPlate)
             
        elsif num == 0
                
            connectPlate = importDLSTSComponentTP1('ConnectPlate',group)  
            entities.transform_entities(Geom::Transformation.new([0,0,100.mm]), connectPlate)
             
        else

            (1..num).each do |i|
                connectPlate = importDLSTSComponentTP1('ConnectPlate',group)  
                entities.transform_entities(Geom::Transformation.new([0,0,i*500.mm - 250.mm]), connectPlate)
            end

        end

        return group
    end

    def createLedger(mod,groupW)               #创建水平杆,模数(0.3,0.6,0.9,1.2,1.5,1.8)

        group = groupW.entities.add_group
        entities = group.entities

        lineAxis = entities.add_line([0,0,0],[-mod.m,0,0])
        circTb = createCircularTube(mod.m - 140.mm,[-70.mm,0,0],X_AXIS,group)
        ledgerEnd = importDLSTSComponentTP1('LedgerEnd',group)
        entities.transform_entities(Geom::Transformation.new([-mod.m,0,0]), ledgerEnd)
        ledgerEnd = importDLSTSComponentTP1('LedgerEnd',group)
        entities.transform_entities(Geom::Transformation.rotation(ORIGIN,Z_AXIS, 180.degrees) , ledgerEnd)
        return group
    end

    def diagonalBrace(mod_h,mod_v,groupW)      #创建竖斜杆,模数(水平杆模数,立杆模数)
    
        group = groupW.entities.add_group
        entities = group.entities
        
        lineAxis = entities.add_line([0,0,0],[-mod_h.m,0,mod_v.m])
        

        p1 = [-56.mm,-56.mm,0]
        p2 = [-mod_h.m+56.mm,-56.mm,mod_v.m]              
        v1 = Geom::Vector3d.new(p1)
        v2 = Geom::Vector3d.new(p2)

        tbLine = entities.add_line(p1,p2)
        distance = p1.distance(p2)

        #puts distance,v2-v1
        circTb = createCircularTube(distance,p1,v1-v2,group)

        ledgerEnd = importDLSTSComponentTP1('LedgerEnd',group)
        entities.transform_entities(Geom::Transformation.new([-mod_h.m,0,mod_v.m]), ledgerEnd)
        entities.transform_entities(Geom::Transformation.rotation([-mod_h.m,0,mod_v.m],Z_AXIS, -45.degrees) , ledgerEnd)
        ledgerEnd = importDLSTSComponentTP1('LedgerEnd',group)
        entities.transform_entities(Geom::Transformation.rotation(ORIGIN,Z_AXIS, 225.degrees) , ledgerEnd)
                
        return group
    
    end

    def baseJack(screwLength,reachLength,groupW)  #创建可调底座（螺杆长度，伸出长度）

        group = groupW.entities.add_group
        entities = group.entities

        lineAxis = entities.add_line([0,0,0],[0,0,screwLength])

        screw = createScrew(screwLength,ORIGIN,Z_AXIS,group)
        base = importDLSTSComponentTP1('base',group)
        adjustNut = importDLSTSComponentTP1('adjustNut',group)   #螺母厚15mm
        entities.transform_entities(Geom::Transformation.new([0,0,reachLength - 15.mm]), adjustNut)
        return group
    end

    def headJack(screwLength,reachLength,groupW)  #创建可调托撑（螺杆长度，伸出长度）

        group = groupW.entities.add_group
        entities = group.entities

        lineAxis = entities.add_line([0,0,0],[0,0,screwLength])

        screw = createScrew(screwLength,ORIGIN,Z_AXIS,group)
        head = importDLSTSComponentTP1('head',group)
        entities.transform_entities(Geom::Transformation.new([0,0,screwLength]), head)
        adjustNut = importDLSTSComponentTP1('adjustNut',group)   #螺母厚15mm
        entities.transform_entities(Geom::Transformation.new([0,0,screwLength - reachLength]), adjustNut)
        return group
    end

    def V_cp(bjl,bjrl,hjl,hjrl,stdArray,groupW)   #创建立杆体系，参数：底托杆长，底托伸出长，顶托杆长，顶托伸出长，立杆模数组
        
        group = groupW.entities.add_group
        entities = group.entities

        baseJack = baseJack(bjl,bjrl,group)
        elevation = bjrl + 100.mm
        #puts elevation/25.4

        $i = 0
        $num = stdArray.length
         
        while $i < $num  do            
            std = createStandard(stdArray[$i],group)
            entities.transform_entities(Geom::Transformation.new([0,0,elevation]), std)
            elevation += stdArray[$i].mm*1000
                    #puts elevation/25.4
            $i +=1
        end

        headJack = headJack(hjl,hjrl,group)
        elevation += hjrl - hjl
        entities.transform_entities(Geom::Transformation.new([0,0,elevation]), headJack)
        
        
        return group

    end

    def singleDL(vcpArray,ldgArray,dgbArray,groupW)   #创建单榀盘扣架体系，参数：立杆数据，水平杆数据，竖斜杆数据

        group = groupW.entities.add_group
        entities = group.entities
        
        bjl = vcpArray.shift.mm*1000
        bjrl = vcpArray.shift.mm*1000

        V_cp(bjl,bjrl,
            vcpArray.shift.mm*1000,vcpArray.shift.mm*1000,
            vcpArray,group)

        elevation = bjrl + 100.mm + 250.mm

        ledger = createLedger(ldgArray.shift,group)
        entities.transform_entities(Geom::Transformation.new([0,0,elevation]), ledger)
         
        while 1 < ldgArray.size  do            
            
            #elevation += 
            ledger = ledger.copy
            z_axis = (ldgArray[1] - ldgArray.shift).mm*500
            entities.transform_entities(Geom::Transformation.new([0,0,z_axis]), ledger)
            #puts z_axis
        end

        if dgbArray.size > 0 

            diagonalBrace = diagonalBrace(dgbArray.shift,dgbArray.shift,group)
            entities.transform_entities(Geom::Transformation.new([0,0,elevation]), diagonalBrace)

            while 1 < dgbArray.size  do            
            
                diagonalBrace = diagonalBrace.copy
                z_axis = (dgbArray[1] - dgbArray.shift).mm*500
                entities.transform_entities(Geom::Transformation.new([0,0,z_axis]), diagonalBrace)
            
            end
        end

        return group
    end





    def createCircularTube(length,center_point,vector,groupW)        #创建圆管

        group = groupW.entities.add_group  #默认群组
    
        dw = 48.3.mm  #立杆外径
        t = 3.2.mm    #立杆壁厚
        dn = dw-t*2   #立杆内径
    
        #center_point = Geom::Point3d.new([0,0,0])
        #vector = Z_AXIS
    
        edges = group.entities.add_circle(center_point, vector, dw/2)               #添加一个直径dw的圆边线
        face = group.entities.add_face(edges)                                       #以边为界添加面
        status = face.pushpull(-length, true)                                       #推拉-length形成向上的圆柱体
    
            edges2 = group.entities.add_circle(center_point, vector, (dw-t*2)/2)    #在圆柱底部画一个直径dn的圆边线
    
            face2 = edges2[0].common_face(edges2[1])                                #利用两边共面引用内径圆的面
            status = face2.pushpull(length, true)                                   #推拉length形成圆环柱
    
            group.entities.erase_entities(face2)                                    #删除内径圆的面
    
        return group
    
    end

    def createScrew(length,center_point,vector,groupW)               #创建螺杆
        
        group = groupW.entities.add_group  #默认群组
        groupHelix = groupW.entities.add_group

        dim = 38.mm
        pitch = 3.mm
        numofscrew = (length/pitch).to_i

        for i in 0..numofscrew-1

            #画上半圆
            new_edges = groupHelix.entities.add_arc([0,0,pitch*i], X_AXIS, [0,0,1], dim/2, 0, 180.degrees)
            curve = new_edges.first.curve
            points = curve.vertices.map(&:position)
            points.each_with_index { |pt, j| pt.z = (j * (pitch/2)/(points.length-1))+ pitch*i}
            curve.move_vertices(points)
            #画下半圆
            new_edges = groupHelix.entities.add_arc([0,0,pitch*i+pitch/2], X_AXIS, [0,0,1], dim/2, 180.degrees, 360.degrees)
            curve = new_edges.first.curve
            points = curve.vertices.map(&:position)
            points.each_with_index { |pt, j| pt.z = (j * pitch/2/(points.length-1)) +pitch/2 + pitch*i }
            curve.move_vertices(points)

        end

        edges = group.entities.add_circle(center_point, vector, dim/2)               #添加一个直径dim的圆边线
        face = group.entities.add_face(edges)                                       #以边为界添加面
        status = face.pushpull(-length, true)                                       #推拉-length形成向上的圆柱体

        #return group
    end


    def importDLSTSComponentTP1(cpName,groupW)   #导入1型组件（不设参数调节的）

        
        def createComponent(transform,path,groupW)
    
            group = groupW.entities.add_group
            entities = group.entities
            definitions = @model.definitions
            componentdefinition = definitions.load path
            instance = entities.add_instance componentdefinition, transform
            return instance
    
        end
    
        @data.each{ |i|
    
            if i[0] == cpName
                return createComponent(i[2],i[1],groupW)
            end
        } 
    
    end


end



##########
##单榀组合测试
##########

group = Sketchup.active_model.active_entities.add_group
entities = group.entities
sgDL = DLSTS.new().singleDL([0.7,0.2,0.6,0.15,3,2,1],[0.9,0,3,6,9,11],[0.9,1.5,0,3,6],group)
sgDL = sgDL.copy
entities.transform_entities(Geom::Transformation.new([-900.mm,0,0]), sgDL)
entities.transform_entities(Geom::Transformation.rotation([-900.mm,0,0],Z_AXIS, -90.degrees) , sgDL)
sgDL = sgDL.copy
entities.transform_entities(Geom::Transformation.new([0,900.mm,0]), sgDL)
entities.transform_entities(Geom::Transformation.rotation([-900.mm,900.mm,0],Z_AXIS, -90.degrees) , sgDL)
sgDL = sgDL.copy
entities.transform_entities(Geom::Transformation.new([900.mm,0,0]), sgDL)
entities.transform_entities(Geom::Transformation.rotation([0,900.mm,0],Z_AXIS, -90.degrees) , sgDL)

sgDL2 = DLSTS.new().singleDL([0.7,0.2,0.6,0.15,3,2,1],[0.9,0,3,6,9,11],[],group)
entities.transform_entities(Geom::Transformation.rotation([0,0,0],Z_AXIS, 180.degrees) , sgDL2)
entities.transform_entities(Geom::Transformation.new([-1800.mm,0,0]), sgDL2)
for i in 0..1

    sgDL2 = sgDL2.copy
    entities.transform_entities(Geom::Transformation.new([0,900.mm,0]), sgDL2)

end
sgDL2 = sgDL2.copy
entities.transform_entities(Geom::Transformation.rotation([-1800.mm,1800.mm,0],Z_AXIS, -90.degrees) , sgDL2)

for i in 0..1

    sgDL2 = sgDL2.copy
    entities.transform_entities(Geom::Transformation.new([900.mm,0,0]), sgDL2)

end

##########
##竖杆组合测试
##########
DLSTS.new().V_cp(700.mm,200.mm,600.mm,150.mm,[3,2,1],Sketchup.active_model.active_entities.add_group)


##########
##油托测试
##########

DLSTS.new().baseJack(700.mm,200.mm,Sketchup.active_model.active_entities.add_group)
DLSTS.new().headJack(600.mm,150.mm)



##########
##竖斜杆测试
##########
DLSTS.new().diagonalBrace(1.8,0.5)
DLSTS.new().diagonalBrace(1.8,1)
DLSTS.new().diagonalBrace(1.8,1.5)
DLSTS.new().diagonalBrace(1.5,2)
DLSTS.new().diagonalBrace(1.2,2.5)
DLSTS.new().diagonalBrace(0.9,2.5)
DLSTS.new().diagonalBrace(0.6,3)

##########
##水平杆测试
##########
DLSTS.new().createLedger(0.3)
DLSTS.new().createLedger(0.6)
DLSTS.new().createLedger(0.9)
DLSTS.new().createLedger(1.2)
DLSTS.new().createLedger(1.5)
DLSTS.new().createLedger(1.8)



##########
##立杆测试
##########
DLSTS.new().createStandard(3)
DLSTS.new().createStandard(2.5)
DLSTS.new().createStandard(2)
DLSTS.new().createStandard(1.5)
DLSTS.new().createStandard(1)
DLSTS.new().createStandard(0.6)
DLSTS.new().createStandard(0.2)


#DLSTS.new().createCircularTube(3000.mm)

#DLSTS.new().importDLSTSComponentTP1('BaseCollar')
#DLSTS.new().importDLSTSComponentTP1('ConnectPlate')



def createStandard(length)   #创建立杆

    model = Sketchup.active_model           #默认模型
    group = model.active_entities.add_group  #默认群组

    dw = 48.3.mm  #外径
    t = 3.2.mm    #壁厚
    dn = dw-t*2   #内径

    center_point = Geom::Point3d.new([0,0,0])
    vector = Z_AXIS

    edges = group.entities.add_circle(center_point, vector, dw/2)               #添加一个直径dw的圆边线
    face = group.entities.add_face(edges)                                       #以边为界添加面
    status = face.pushpull(-length, true)                                       #推拉-length形成向上的圆柱体

        edges2 = group.entities.add_circle(center_point, vector, (dw-t*2)/2)    #在圆柱底部画一个直径dn的圆边线

        face2 = edges2[0].common_face(edges2[1])                                #利用两边共面引用内径圆的面
        status = face2.pushpull(length, true)                                   #推拉length形成圆环柱

        group.entities.erase_entities(face2)                                    #删除内径圆的面

        return group

end

createStandard(3000.mm)


def createBaseCollar(transform,path)    #创建基座

    model = Sketchup.active_model
    entities = model.active_entities

    definitions = model.definitions
    componentdefinition = definitions.load path
    instance = entities.add_instance componentdefinition, transform
    return instance
end

createBaseCollar([-27.35.mm,-27.35.mm,-100.mm],"c:/baseCollar.skp")

def createConnectPlate(transform,path)    #创建连接盘

    model = Sketchup.active_model
    entities = model.active_entities

    definitions = model.definitions
    componentdefinition = definitions.load path
    instance = entities.add_instance componentdefinition, transform
    return instance
end

createConnectPlate([-69.5.mm,-69.5.mm,-5.mm],"c:/connectingPlate.skp")


def importDLSTSComponent(cpName)
    
    data = [
        ['BaseCollar','c:/baseCollar.skp',[-27.35.mm,-27.35.mm,-100.mm]],
        ['ConnectPlate','c:/connectingPlate.skp',[-69.5.mm,-69.5.mm,-5.mm]]
    ]

    def createComponent(transform,path)

        model = Sketchup.active_model
        entities = model.active_entities
        definitions = model.definitions
        componentdefinition = definitions.load path
        instance = entities.add_instance componentdefinition, transform
        return instance

    end

    data.each{ |i|

        if i[0] == cpName
            return createComponent(i[2],i[1])
        end
    } 

end

importDLSTSComponent('BaseCollar')
importDLSTSComponent('ConnectPlate')